<template>
  <div class="wrap">
    <div class="page-head">
      <a-breadcrumb>
        <a-breadcrumb-item>{{$t('role.role.60zejux960k0')}}</a-breadcrumb-item>
        <a-breadcrumb-item>{{$t('role.role.60zejux99ks0')}}</a-breadcrumb-item>
      </a-breadcrumb>
    </div>
    <c-table-search :loading="form.loading" v-model:model="form.data" @refresh="getData">
      <a-col :span="8">
        <a-form-item field="keywords" :label="$t('role.role.60zejux99x80')">
          <a-input v-model="form.data.keywords" :placeholder="$t('role.role.60zejux9a2s0')" />
        </a-form-item>
      </a-col>
      <template v-slot:refresh>
        <a-button type="outline" @click="refresh">
          <template #icon>
            <icon-loop />
          </template>
        </a-button>
      </template>
      <template v-slot:add>
        <a-button type="primary" v-permission="['role:add']"  @click="form.create.show = true">
          <template #icon>
            <icon-plus />
          </template>
          {{$t('role.role.60zejux9a9s0')}}
        </a-button>
      </template>
    </c-table-search>
    <div class="table-scroll">
      <c-table :list="form.list" :loading="form.loading" :count="form.count" v-model:data="form.data"
        @refresh="getData">
        <a-table-column :title="$t('role.role.60zejux9ccw0')" ellipsis tooltip :width="100">
          <template #cell="{ rowIndex }">
            {{ rowIndex + 1 }}
          </template>
        </a-table-column>
        <a-table-column :title="$t('role.role.60zejux99x80')" ellipsis tooltip :width="500">
          <template #cell="{ record }">
            {{ record.name? record.name : '-' }}
          </template>
        </a-table-column>
        <a-table-column :title="$t('role.role.60zejux9cm40')" ellipsis tooltip :width="500">
          <template #cell="{ record }">
            {{ record.remark? record.remark : '-' }}
          </template>
        </a-table-column>
        <a-table-column :ellipsis="true" :title="$t('role.role.60zejux9crc0')" :width="300">
          <template #cell="{ record }">
            {{ record.createTime ? dayjs.unix(record.createTime).format('YYYY-MM-DD HH:mm:ss') : '-' }}
          </template>
        </a-table-column>
        <a-table-column :title="$t('role.role.60zejux9cys0')" fixed='right' :width="180">
          <template #cell="{ record }">
            <a-space>
              <a-button type="text" v-permission="['role:edit']"  @click="handleUpdata(record)">{{$t('role.role.60zejux9d440')}}</a-button>
              <a-button type="text" v-permission="['role:delete']" status="danger" @click="handleDelete(record)">{{$t('role.role.60zejux9d900')}}</a-button>
            </a-space>
          </template>
        </a-table-column>
      </c-table>
      <Create @refresh="getData"  v-model:show="form.create.show" :auths="auths" :allAuths="form.allAuths"></Create>
      <Updata @refresh="getData" v-model:show="form.update.show" v-model:data="form.update.data" :auths="auths"
        :allAuths="form.allAuths"></Updata>
    </div>
  </div>
</template>

<script lang='ts' setup>
import { apiRoleList, apiRoleDelete, apiAuthList } from '@/api/setting'
import dayjs from 'dayjs'
const Create = defineAsyncComponent(() => import('./components/create.vue'))
const Updata = defineAsyncComponent(() => import('./components/updata.vue'))
const emit = defineEmits(['refresh'])
const refresh = () => {
  emit('refresh')
}
const form = reactive({
  create: {
    show: false
  },
  update: {
    loading: false,
    show: false,
    data: {},
  },
  data: {
    keywords: '',
    page: 1,
    pageSize: 20,
  },
  auths: [],
  allAuths: [],
  loading: false,
  list: [],
  count: 0
})
// 角色列表
const getData = async () => {
  form.loading =  true
  const { code, data } = await apiRoleList({
    ...form.data,
    keywords: form.data.keywords
  })
  form.loading = false
  if (code != 200) return;
  form.list = data.list
  form.count = data.total
}
//权限列表
const filterAuths = (authList: any) => {
  const filteredAuth = authList?.map((auth: any) => {
    auth.menuIcon = auth.icon;
    delete auth.icon
    if (auth?.children?.length) {
      auth.children = filterAuths(auth.children);
    }
    return auth
  })
  return filteredAuth
}
const auths: any = computed(() => {
  return filterAuths(cloneDeep(form.auths))
})
const getAuthsData = async () => {
  const { code, data } = await apiAuthList({
    ...form.data
  })
  if (code != 200) return;
  form.auths = data.list
  form.allAuths = useTreeAllId(form.auths)
}
// 编辑
const handleUpdata = (record: any) => {
  form.update.data = cloneDeep(record)
  form.update.show = true
}
// 删除
const handleDelete = async (record: any) => {
  useModal.warning({
    content: t('role.role.60zejux9dgo0'),
    onBeforeOk: async () => {
      const { code } = await apiRoleDelete({
        id: record.id
      })
      if (code != 200) return false;
      useMessage.success()
      getData()
      return true
    }
  })
}
onBeforeMount(() => {
  getData()
  getAuthsData()
})
</script>

<style scoped lang="less"></style>
